In [1]:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import cv2
import numpy as np
%matplotlib inline
# Read in the image
image = mpimg.imread(fname='images/curved_lane.jpg')
plt.imshow(X=image)
Out[1]:
In [14]:
# Convert to grayscale for filtering
gray = cv2.cvtColor(src=image,
code=cv2.COLOR_RGB2GRAY)
plt.imshow(X=gray, cmap='gray')
Out[14]:
Below, you've been given one common type of edge detection filter: a Sobel operator.
The Sobel filter is very commonly used in edge detection and in finding patterns in intensity in an image. Applying a Sobel filter to an image is a way of taking (an approximation) of the derivative of the image in the x or y direction, separately. The operators look as follows.
It's up to you to create a Sobel x operator and apply it to the given image.
For a challenge, see if you can put the image through a series of filters: first one that blurs the image (takes an average of pixels), and then one that detects the edges.
In [3]:
# Create a custom kernel
# 3x3 array for edge detection
sobel_y = np.array([[ -1, -2, -1],
[ 0, 0, 0],
[ 1, 2, 1]])
## TODO: Create and apply a Sobel x operator
sobel_x = np.array([[ -1, 0, 1],
[ -2, 0, 2],
[ -1, 0, 1]])
# Filter the image using filter2D, which has inputs: (grayscale image, bit-depth, kernel)
filtered_image = cv2.filter2D(src=gray,
ddepth=-1,
kernel=sobel_y)
filtered_image2 = cv2.filter2D(src=gray,
ddepth=-1,
kernel=sobel_x)
plt.imshow(X=filtered_image,
cmap='gray')
Out[3]:
In [4]:
plt.imshow(X=filtered_image2,
cmap='gray')
Out[4]:
In [5]:
image2 = mpimg.imread(fname='images/white_lines.jpg')
gray2 = cv2.cvtColor(src=image2,
code=cv2.COLOR_RGB2GRAY)
In [6]:
sample_filter = np.array([[ 0, 0, 2, 0, 0],
[ 0, 0, 1, 0, 0],
[ 2, -1, 1, -1, 2],
[ 0, 0, 1, 0, 0],
[ 0, 0, 2, 0, 0]])
In [7]:
filtered_image3 = cv2.filter2D(src=gray2,
ddepth=-1,
kernel=sample_filter)
In [8]:
plt.imshow(X=gray2,
cmap='gray')
Out[8]:
In [11]:
plt.imshow(X=filtered_image3,
cmap='gray')
Out[11]: